﻿using System;
using System.Collections.Generic;
using Moq;
using NUnit.Framework;
using Xenta.Enums;
using Xenta.Operations;
using Xenta.Plugins;
using Xenta.Structs;
using Xenta.Utils;

namespace Xenta.Tests.Operations
{
    [TestFixture]
    public class MessageTemplateTests : OperationTest
    {
        protected override IEnumerable<Type> ModelMappings
        {
            get
            {
                return NHMappings.ALL;
            }
        }

        protected override IDictionary<DependencyInfo, IDependency> Dependencies
        {
            get
            {
                var components = new Dictionary<DependencyInfo, IDependency>();
                var messageSender = new Mock<IMessageSender>();
                components.Add(new DependencyInfo
                {
                    ID = "52fa95dc-61b3-47b8-af7d-9453d8269d30"
                }, messageSender.Object);
                return components;
            }
        }

        [TestCase("xy", "from", "to", "bcc", "reply")]
        public void Can_CRUD(string code, string from, string to, string bccTo, string replyTo)
        {
            var template = Processor.Execute(new CreateMessageTemplate
            {
                SenderID = Guid.Parse("52fa95dc-61b3-47b8-af7d-9453d8269d30"),
                Code = code,
                From = from,
                To = to,
                BccTo = bccTo,
                ReplyTo = replyTo,
                Flags = MessageTemplateFlags.None 
            }).Result;

            Assert.NotNull(template);
            template.Code.AreEqualIgnoringCase(code);
            template.From.AreEqualIgnoringCase(from);
            template.To.AreEqualIgnoringCase(to);
            template.BccTo.AreEqualIgnoringCase(bccTo);
            template.ReplyTo.AreEqualIgnoringCase(replyTo);
            template.Flags.AreEqual(MessageTemplateFlags.None);
            template.AreEqual(Processor.Execute(new GetMessageTemplate
            {
                EntityID = template.EntityID
            }).Result);
            template.AreEqual(Processor.Execute(new GetMessageTemplateByCode
            {
                Code = code
            }).Result);

            code = code.Reverse();
            from = from.Reverse();
            to = to.Reverse();
            bccTo = bccTo.Reverse();
            replyTo = replyTo.Reverse();
            var upd_template = Processor.Execute(new UpdateMessageTemplate
            {
                EntityID = template.EntityID,
                SenderID = Guid.Parse("52fa95dc-61b3-47b8-af7d-9453d8269d30"),
                Code = code,
                From = from,
                To = to,
                BccTo = bccTo,
                ReplyTo = replyTo,
                Flags = MessageTemplateFlags.Active 
            }).Result;

            Assert.NotNull(upd_template);
            upd_template.AreEqual(template);
            upd_template.Code.AreEqualIgnoringCase(code);
            upd_template.From.AreEqualIgnoringCase(from);
            upd_template.To.AreEqualIgnoringCase(to);
            upd_template.BccTo.AreEqualIgnoringCase(bccTo);
            upd_template.ReplyTo.AreEqualIgnoringCase(replyTo);
            upd_template.Flags.AreEqual(MessageTemplateFlags.Active);

            Processor.Execute(new DeleteMessage
            {
                EntityID = template.EntityID
            });
        }

        [Test]
        public void Cannot_Create_If_Code_Empty()
        {
            Assert.Throws<ApplicationException>(() => Processor.Execute(new CreateMessageTemplate
            {
                Code = String.Empty
            }));
        }

        [Test]
        public void Cannot_Update_If_Code_Empty()
        {
            Assert.Throws<ApplicationException>(() => Processor.Execute(new UpdateMessageTemplate
            {
                Code = String.Empty
            }));
        }
    }
}
